﻿/****************************************************************************
 * Description: 
 * 
 * Document: https://github.com/hiramtan/HiProtobuf
 * Author: hiramtan@live.com
 ****************************************************************************/
using System.IO;

namespace CSharpGeneratorForProton
{
    internal class LanguageGenerater
    {
        private string _languageFolder;
        public void Process()
        {
            if (string.IsNullOrEmpty(Settings.Export_Folder))
                return;

            _languageFolder = Path.Combine(Settings.Export_Folder, Settings.gen_folder);
            CheckFolder(_languageFolder);

            var protoFolder = Path.Combine(Settings.Export_Folder, Settings.proto_folder);
            //Process_csharp(protoFolder);
            //Process_cpp(protoFolder);
            //Process_go(protoFolder);
            //Process_java(protoFolder);
            //Process_python(protoFolder);
            Process_lua(protoFolder);
        }

        private void CheckFolder(string folder)
        {
            if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
            }
        }

        private void Process_csharp(string protoPath)
        {
            var outFolder = Path.Combine(_languageFolder, Settings.csharp_folder);
            CheckFolder(outFolder);
            //递归查询
            string[] files = Directory.GetFiles(protoPath, "*.proto", SearchOption.AllDirectories);
            for (int i = 0; i < files.Length; i++)
            {
                var filePath = files[i];
                var command = Settings.Protoc_Path + string.Format(" -I={0} --csharp_out={1} {2}", protoPath, outFolder, filePath);
                var log = Common.Cmd(command);
            }
        }

        private void Process_cpp(string protoPath)
        {
            var outFolder = Path.Combine(_languageFolder, Settings.cpp_folder);
            CheckFolder(outFolder);
            //递归查询
            string[] files = Directory.GetFiles(protoPath, "*.proto", SearchOption.AllDirectories);
            for (int i = 0; i < files.Length; i++)
            {
                var filePath = files[i];
                var command = Settings.Protoc_Path + string.Format(" -I={0} --cpp_out={1} {2}", protoPath, outFolder, filePath);
                var log = Common.Cmd(command);
            }
        }

        private void Process_go(string protoPath)
        {
            var outFolder = Path.Combine(_languageFolder, Settings.go_folder);
            CheckFolder(outFolder);
            //递归查询
            string[] files = Directory.GetFiles(protoPath, "*.proto", SearchOption.AllDirectories);
            for (int i = 0; i < files.Length; i++)
            {
                var filePath = files[i];
                var command = Settings.Protoc_Path + string.Format(" -I={0} --go_out={1} {2}", protoPath, outFolder, filePath);
                var log = Common.Cmd(command);
            }
        }

        private void Process_java(string protoPath)
        {
            var outFolder = Path.Combine(_languageFolder, Settings.java_folder);
            CheckFolder(outFolder);
            //递归查询
            string[] files = Directory.GetFiles(protoPath, "*.proto", SearchOption.AllDirectories);
            for (int i = 0; i < files.Length; i++)
            {
                var filePath = files[i];
                var command = Settings.Protoc_Path + string.Format(" -I={0} --java_out={1} {2}", protoPath, outFolder, filePath);
                var log = Common.Cmd(command);
            }
        }

        private void Process_python(string protoPath)
        {
            var outFolder = Path.Combine(_languageFolder, Settings.python_folder);
            CheckFolder(outFolder);
            //递归查询
            string[] files = Directory.GetFiles(protoPath, "*.proto", SearchOption.AllDirectories);
            for (int i = 0; i < files.Length; i++)
            {
                var filePath = files[i];
                var command = Settings.Protoc_Path + string.Format(" -I={0} --python_out={1} {2}", protoPath, outFolder, filePath);
                var log = Common.Cmd(command);
            }
        }

        private void Process_lua(string protoPath)
        {
            var outFolder = Path.Combine(_languageFolder, Settings.lua_folder);
            CheckFolder(outFolder);
            var protLuaPath = Settings.Protoc_Lua_Path;
            //递归查询
            string[] files = Directory.GetFiles(protoPath, "*.proto", SearchOption.AllDirectories);
            for (int i = 0; i < files.Length; i++)
            {
                var filePath = files[i];
                var command = Settings.Protoc_Path + string.Format(" -I={0} --plugin=protoc-gen-lua={1} --lua_out={2} {3}", protoPath, protLuaPath, outFolder, filePath);
                var log = Common.Cmd(command);
                //Logger.Info(log);
            }
        }
    }
}
